package com.tyk.bookstore.back.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.util.ObjUtil;
import com.tyk.bookstore.back.product.model.dto.Book4StatisticDto;
import com.tyk.bookstore.back.product.model.entity.BookDo;
import com.tyk.bookstore.back.product.model.entity.BookStatisticDo;
import com.tyk.bookstore.back.product.model.enume.BookSortOrder;
import com.tyk.bookstore.back.product.model.query.BookStatisticAddQuery;
import com.tyk.bookstore.back.product.model.query.BookStatisticPageQuery;
import com.tyk.bookstore.back.product.model.query.BookStatisticUpdateQuery;
import com.tyk.bookstore.back.product.model.vo.BookStatisticPageVo;
import com.tyk.bookstore.back.product.model.vo.BookStatisticVo;
import com.tyk.bookstore.back.product.repository.BookRepository;
import com.tyk.bookstore.back.product.repository.BookStatisticRepository;
import com.tyk.bookstore.back.product.service.BookStatisticService;
import com.tyk.bookstore.back.product.util.MapStructProduct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author tyk
 * @description 书籍统计信息
 */
@Service
@RequiredArgsConstructor

public class BookStatisticServiceImpl implements BookStatisticService {

    private final BookStatisticRepository bookStatisticRepository;
    private final BookRepository bookRepository;
    private final MapStructProduct mapStructProduct;

    @Override
    public BookStatisticVo getById(Long id) {
        BookStatisticDo bookStatistic = bookStatisticRepository.selectById(id);
        BookStatisticVo bookStatisticVo = mapStructProduct.toBookStatisticVo(bookStatistic);
        return bookStatisticVo;
    }

    @Override
    public BookStatisticPageVo getPage(BookStatisticPageQuery query) {
        // 分页查询
        Page<BookStatisticDo> page = new Page<>(query.getPage(), query.getLimit());
        LambdaQueryWrapper<BookStatisticDo> w = buildPageQueryWrapper(query);
        bookStatisticRepository.selectPage(page, w);
        BookStatisticPageVo pv = mapStructProduct.toBookStatisticPageVo(page);
        // 书籍信息
        Set<Long> bidSet = page.getRecords().stream().map(BookStatisticDo::getBookId).collect(Collectors.toSet());
        if (ObjUtil.isEmpty(bidSet)) return PageVo.instance(BookStatisticPageVo.class);
        Map<Long, Book4StatisticDto> bMap = bookRepository
                .selectBook4StatisticDtoListByIdSet(bidSet, bidSet.size())
                .stream().collect(Collectors.toMap(
                        Book4StatisticDto::getId,
                        it -> it
                ));
        pv.getRecords().forEach(it -> {
            Book4StatisticDto dto = bMap.get(it.getBookId());
            it.setBookNo(dto.getBookNo())
                    .setBookIsbn(dto.getIsbn())
                    .setBookName(dto.getName());
        });
        // 返回
        return pv;
    }


    @Override
    public void add(BookStatisticAddQuery query) {
        BookStatisticDo bookStatistic = mapStructProduct.toBookStatisticDo(query);
        bookStatisticRepository.insert(bookStatistic);
    }

    @Override
    public void updateById(BookStatisticUpdateQuery query) {
        BookStatisticDo bookStatistic = mapStructProduct.toBookStatisticDo(query);
        bookStatisticRepository.updateById(bookStatistic);
    }

    @Override
    public void deleteById(Long id) {
        bookStatisticRepository.deleteById(id);
    }

    @Override
    public void deleteBatchByIdSet(Set<Long> idSet) {
        bookStatisticRepository.deleteBatchIds(idSet);
    }


    /**
     * 构建分页查询Wrapper
     */
    private LambdaQueryWrapper<BookStatisticDo> buildPageQueryWrapper(BookStatisticPageQuery query) {

        LambdaQueryWrapper<BookStatisticDo> w = new LambdaQueryWrapper<>();

        // 购买人数
        w.ge(query.getSaleNumFrom() != null, BookStatisticDo::getSaleNum, query.getSaleNumFrom());
        w.le(query.getSaleNumTo() != null, BookStatisticDo::getSaleNum, query.getSaleNumTo());

        // 支付人数
        w.ge(query.getPayNumFrom() != null, BookStatisticDo::getPayNum, query.getPayNumFrom());
        w.le(query.getPayNumTo() != null, BookStatisticDo::getPayNum, query.getPayNumTo());

        // 收藏人数
        w.ge(query.getCollectNumFrom() != null, BookStatisticDo::getCollectNum, query.getCollectNumFrom());
        w.le(query.getCollectNumTo() != null, BookStatisticDo::getCollectNum, query.getCollectNumTo());

        // 评论人数
        w.ge(query.getCommentNumFrom() != null, BookStatisticDo::getCommentNum, query.getCommentNumFrom());
        w.le(query.getCommentNumTo() != null, BookStatisticDo::getCommentNum, query.getCommentNumTo());

        // 点赞人数
        w.ge(query.getLikeNumFrom() != null, BookStatisticDo::getLikeNum, query.getLikeNumFrom());
        w.le(query.getLikeNumTo() != null, BookStatisticDo::getLikeNum, query.getLikeNumTo());

        // 价格
        w.ge(query.getPayNumFrom() != null, BookStatisticDo::getPrice, query.getPayNumFrom());
        w.le(query.getPriceTo() != null, BookStatisticDo::getPrice, query.getPriceTo());

        // 排序条件
        if (query.getSortOrder() == null) query.setSortOrder(BookSortOrder.MULTIPLE);
        if (query.getDesc() == null) query.setDesc(true);
        w.orderBy(true, !query.getDesc(), query.getSortOrder().toField());

        // 书籍条件
        LambdaQueryWrapper<BookDo> bw = Wrappers.lambdaQuery(BookDo.class);
        bw.eq(ObjUtil.notBlank(query.getKeyword()), BookDo::getTitle, query.getKeyword());
        bw.like(ObjUtil.notBlank(query.getBookName()), BookDo::getName, query.getBookName());
        if (ObjUtil.notBlank(query.getBookNoOrIsbn())) {
            bw.and(it -> {
                it.eq(BookDo::getBookNo, query.getBookNoOrIsbn()).or().eq(BookDo::getIsbn, query.getBookNoOrIsbn());
            });
        }

        return w;
    }

}

